package com.example.service.user.impl;

import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.example.dto.business.DynamicCommentBusinessDTO;
import com.example.dto.request.community.DynamicCommentRequestDTO;
import com.example.dto.request.PageRequestDTO;
import com.example.exception.InsertException;
import com.example.exception.community.DynamicNotExistException;
import com.example.mapper.UserDynamicCommentMapper;
import com.example.mapper.UserDynamicMapper;
import com.example.pojo.UserDynamic;
import com.example.pojo.UserDynamicComment;
import com.example.service.user.CommunityDynamicCommentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author LeXin Huang
 * @date 2021年03月29日 16:52
 */
@Service
public class CommunityDynamicCommentServiceImpl implements CommunityDynamicCommentService {

    private final UserDynamicCommentMapper commentMapper;

    private final UserDynamicMapper dynamicMapper;

    public CommunityDynamicCommentServiceImpl(UserDynamicCommentMapper commentMapper,
                                              UserDynamicMapper dynamicMapper) {
        this.commentMapper = commentMapper;
        this.dynamicMapper = dynamicMapper;
    }

    @Transactional(rollbackFor = InsertException.class)
    @Override
    public void addComment(DynamicCommentRequestDTO commentRequestDTO) {
        UserDynamic dynamic = dynamicMapper.selectById(commentRequestDTO.getDynamicId());
        if (null == dynamic) {
            throw new DynamicNotExistException("这个动态不见了哦!");
        }
        UserDynamicComment dynamicComment = new UserDynamicComment();
        dynamicComment.setContent(commentRequestDTO.getComment());
        dynamicComment.setDynamicId(commentRequestDTO.getDynamicId());
        dynamicComment.setUserId(commentRequestDTO.getUserId());
        if (!SqlHelper.retBool(commentMapper.insert(dynamicComment))) {
            throw new InsertException("发表动态失败! 请稍后再试!");
        }
    }

    @Override
    public List<DynamicCommentBusinessDTO> getDynamicHistoryComments(Integer dynamicId, PageRequestDTO pageRequestDTO) {
        UserDynamic dynamic = dynamicMapper.selectById(dynamicId);
        if (null == dynamic) {
            throw new DynamicNotExistException("这个动态不见了哦!");
        }
        return transformDTOList(commentMapper.selectHistoryComments(dynamicId, pageRequestDTO.getOffset(), pageRequestDTO.getSize()));
    }

    /**
     * 将 PO 原型对象转换为业务处理对象 DTO, 并确保返回的结果非 null
     * @param queryRes dao查询结果集合
     * @return dto封装对象集合, 保证不为 null
     */
    private List<DynamicCommentBusinessDTO> transformDTOList(List<UserDynamicComment> queryRes) {
        if (null == queryRes) {
            return Collections.emptyList();
        }
        List<DynamicCommentBusinessDTO> res = new ArrayList<>();
        for (UserDynamicComment comment : queryRes) {
            res.add(new DynamicCommentBusinessDTO(comment));
        }
        return res;
    }
}
